package ControlLayer;

import java.util.ArrayList;

import DBLayer.DBConnection;

import DBLayer.DBProduct;

import DBLayer.IFDBProduct;

import ModelLayer.Product;

public class ProductCtr {
	private SupplierCtr supplierCtr;

	public ProductCtr() {
		supplierCtr = new SupplierCtr();
	}

	public Product getProduct(int productId) {
		IFDBProduct dbProduct = new DBProduct();
		return dbProduct.getProduct(productId, true);
	}

	public void insertProduct(String name, double payPrice, double salePrice,
			double rentPrice, String countryOfOrigin, int currentStock,
			int minStock, boolean isBelowMin, int clothSize, int supplierId,
			String color, String equipmentType, String description,
			String fabric, String calibre) {
		Product product = new Product();
		product.setName(name);
		product.setPayPrice(payPrice);
		product.setSalePrice(salePrice);
		product.setRentPrice(rentPrice);
		product.setCountryOfOrigin(countryOfOrigin);
		product.setCurrentStock(currentStock);
		product.setMinStock(minStock);
		product.setBelowMin(isBelowMin);
		product.setClothSize(clothSize);
		product.setColor(color);
		product.setEquipmentType(equipmentType);
		product.setDescription(description);
		product.setFabric(fabric);
		product.setCalibre(calibre);
		product.setSupplier(supplierCtr.getSupplier(supplierId));
		try {
			DBConnection.startTransaction();
			DBProduct dbProduct = new DBProduct();
			dbProduct.insertProduct(product);
			DBConnection.commitTransaction();
		} catch (Exception e) {
			DBConnection.rollbackTransaction();
		}
	}

	public int updateProduct(int productId, String name, double payPrice,
			double salePrice, double rentPrice, String countryOfOrigin,
			int currentStock, int minStock, boolean isBelowMin, int clothSize,
			int supplierId, String color, String equipmentType,
			String description, String fabric, String calibre) {
		IFDBProduct dbProduct = new DBProduct();
		Product product = new Product();
		product.setName(name);
		product.setPayPrice(payPrice);
		product.setSalePrice(salePrice);
		product.setRentPrice(rentPrice);
		product.setCountryOfOrigin(countryOfOrigin);
		product.setCurrentStock(currentStock);
		product.setMinStock(minStock);
		product.setBelowMin(isBelowMin);
		product.setClothSize(clothSize);
		product.setColor(color);
		product.setEquipmentType(equipmentType);
		product.setDescription(description);
		product.setFabric(fabric);
		product.setCalibre(calibre);
		product.setSupplier(supplierCtr.getSupplier(supplierId));
		return dbProduct.updateProduct(product);
	}

	public ArrayList<Product> getAllProduct() {
		IFDBProduct dbProduct = new DBProduct();
		ArrayList<Product> products = new ArrayList<Product>();
		products = dbProduct.getAllProduct(false);
		return products;
	}

}
